package com.weixin.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.weixin.service.IZhuanZhangService;
import com.weixin.service.impl.ZhuanZhangServiceImpl;
import com.weixin.util.PageUtil;
import com.weixin.vo.Bank;
import com.weixin.vo.Users;
import com.weixin.vo.ZhuanZhang;

@WebServlet("/ZhuanZhangServlet/*")
public class ZhuanZhangServlet extends HttpServlet{
	IZhuanZhangService zhuanService = new ZhuanZhangServiceImpl();
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pathInfo = request.getPathInfo().substring(1);
		if("listAll".equals(pathInfo)){
            listAll(request,response);
        }else if ("findAllBank".equals(pathInfo)) {
        	findAllBank(request,response);
		}else if("findALLBySplit".equals(pathInfo)) {
			findALLBySplit(request,response);
		}else if("removeById".equals(pathInfo)) {
			removeById(request,response);
		}else if("removeAll".equals(pathInfo)) {
			removeAll(request,response);
		}else if("listMemberInfo".equals(pathInfo)) {
			listMemberInfo(request,response);
		}else if("insert".equals(pathInfo)) {
			insert(request,response);
		}
	}
	
	/**
	 * 根据姓名和密码对数据修改后然后再加入数据库里
	 * @param request
	 * @param response
	 */
	private void insert(HttpServletRequest request, HttpServletResponse response) {
		try {
			//设置编码
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			//获取值
			String user = request.getParameter("name");
			String pwd = request.getParameter("user_pass");
			String bank_name = request.getParameter("bank_name");
			int money1 =  0;
			if(request.getParameter("user_money")!=null&&!("".equals(request.getParameter("user_money")))) {
				money1 = Integer.valueOf(request.getParameter("user_money"));
			}
			int money2= 0;
			if(request.getParameter("money")!=null&&!("".equals(request.getParameter("money")))) {
				money2 = Integer.valueOf(request.getParameter("money"));
			}
			int user_money = money1 + money2;
			//创建容器放值
			ZhuanZhang vo = new ZhuanZhang();
			Users vo1 = new Users();
			vo.setUser_name(user);
			vo.setBank_name(bank_name);
			vo.setMoney(money2);
			vo.setUser_money(user_money);
			vo.setTime(new Date());
			
			vo1.setUser_name(user);
			vo1.setUser_pwd(pwd);
			vo1.setUser_money(user_money);
			if(money2==0) {
				response.getWriter().println(2);
			}
			else if(zhuanService.insert(vo)&&zhuanService.update(vo1)) {
				response.getWriter().println(1);
			}else {
				response.getWriter().println(0);
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
     * 带搜索分页
     * @param pageNum   页号
     * @param pageSize  页面大小
     * @param paramMap  所有参数
     * @return
     * @throws SQLException
     */
	private void listMemberInfo(HttpServletRequest request, HttpServletResponse response) {
		//获取会员姓名
		String user_name = request.getParameter("user_name");
		//设置默认显示第一页
		int pageNum = 1;
		if(request.getParameter("pageNum")!=null) {
			pageNum = Integer.valueOf(request.getParameter("pageNum"));
		}
		try {
			List<ZhuanZhang> zhuanzhang = zhuanService.selectMemberInfo(user_name,pageNum,PageUtil.PAGEUTIL);
			//调用业务层的方法获取一共有多少个会员
			long count = zhuanService.selectMemberCount(user_name);
			//计算一共有几页
			int pageCount = (int)Math.ceil(count*1.0/PageUtil.PAGEUTIL);
			//request绑定属性
			request.setAttribute("zhuanzhangList", zhuanzhang);
			//绑定user_name进行表单回填
			request.setAttribute("user_name", user_name);
			request.setAttribute("pageNum",pageNum);
			request.setAttribute("pageCount",pageCount);
			request.getRequestDispatcher("/pages/bank/basicAccount_list.jsp").forward(request, response);
		} catch (Exception e) {
				e.printStackTrace();
		}
	}
	
	/**
	 * 通过点击框获得一个关于单号的数组，根据数组里的值删除数据
	 * @param request
	 * @param response
	 */
	private void removeAll(HttpServletRequest request, HttpServletResponse response) {
		 String[] trade_ids = request.getParameterValues("trade_id");
	        try {
	            if(zhuanService.removeAll(trade_ids)){
	                response.getWriter().println(1);
	            }else{
	                response.getWriter().println(0);
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	}
	
	/**
	 * 根据单号删除信息
	 * @param request
	 * @param response
	 */
	private void removeById(HttpServletRequest request, HttpServletResponse response) {
		int trade_id = Integer.valueOf(request.getParameter("trade_id"));
		try {
			if(zhuanService.removeById(trade_id)) {
				//删除成功
				response.getWriter().println(1);
			}else {
				//删除失败
				response.getWriter().println(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 分页
	 * @param request
	 * @param response
	 */
	private void findALLBySplit(HttpServletRequest request, HttpServletResponse response) {
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		try {
			List<ZhuanZhang> zhuanzhang = zhuanService.findBySplit(pageNum, PageUtil.PAGEUTIL);
			 //计算总条数;
            int total = zhuanService.findTotal();
            //计算总页数
            int pageCount = (int)Math.ceil(total*1.0/PageUtil.PAGEUTIL);
            // reques对象绑定属性员工list
            request.setAttribute("pageCount",pageCount);
            request.setAttribute("pageNum",pageNum);
            request.setAttribute("zhuanzhangList",zhuanzhang);
            //转发到jsp处理页面
            request.getRequestDispatcher("/pages/bank/basicAccount_list.jsp").forward(request,response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询所有的银行 并遍历
	 * @param request
	 * @param response
	 */
	private void findAllBank(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Bank> bankList = zhuanService.findALl();
            request.setAttribute("bankList",bankList);
            //转发到jsp处理页面
            request.getRequestDispatcher("/pages/bank/kiting.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
		
	}
	
	/**
	 * 查询所有的数据
	 * @param request
	 * @param response
	 */
	private void listAll(HttpServletRequest request, HttpServletResponse response) {
		try {
            /*拿到转化数据里所有的信息*/
           List<ZhuanZhang> zhuanzhang = zhuanService.selectAll();
            request.setAttribute("zhuanzhangList",zhuanzhang);
            //转发到jsp处理页面
            request.getRequestDispatcher("/pages/bank/kiting.jsp").forward(request,response);

        } catch (Exception e) {
            e.printStackTrace();
        }
		
	}
	
}
